//
//  KATSwitch.m
//  KATFramework
//
//  Created by Kantice on 14/11/6.
//  Copyright (c) 2014年 KatApp. All rights reserved.
//

#import "KATSwitch.h"

@implementation KATSwitch

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



+ (instancetype)switchWithFrame:(CGRect)frame
{
    KATSwitch *sw=[[[self alloc] init] autorelease];
        
    sw.frame=frame;
    sw.backgroundColor=[UIColor clearColor];
    sw.offColor=COLOR(COLOR_G85);
    sw.onColor=COLOR(COLOR_G40);
    sw.buttonColor=COLOR(COLOR_SMOKE);
    sw.buttonBorderWidth=0.5f;
    sw.animateTime=0.32f;
    sw.lineHeight=2.0f;
    sw.hasTapEvent=YES;
    
    return sw;
}


- (void)initSwitch
{
    
    _line=[[UIView alloc] init];
    _line.frame=CGRectMake(0, (self.frame.size.height-_lineHeight)*0.5, self.frame.size.width, _lineHeight);
    _line.backgroundColor=_offColor;
    _line.layer.cornerRadius=_lineHeight*0.5;
    [self addSubview:_line];
    
    _button=[[UIView alloc] init];
    _button.frame=CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    _button.backgroundColor=_buttonColor;
    _button.layer.borderColor=_offColor.CGColor;
    _button.layer.borderWidth=_buttonBorderWidth;
    _button.layer.cornerRadius=self.frame.size.height*0.5;
    [self addSubview:_button];
    
    _point=[[UIView alloc] init];
    _point.frame=CGRectMake((self.frame.size.height-_lineHeight*1.6)*0.5, (self.frame.size.height-_lineHeight*1.6)*0.5, _lineHeight*1.6, _lineHeight*1.6);
    _point.backgroundColor=_offColor;
    _point.layer.cornerRadius=_lineHeight*0.8;
    [_button addSubview:_point];
    
    _isOn=NO;
    
    if(_hasTapEvent)//点击事件
    {
        UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(switchTap)];
        singleTap.numberOfTapsRequired=1;
        [self addGestureRecognizer:singleTap];
        [singleTap release];
        singleTap=nil;
    }
}


//点击事件
- (void)switchTap
{
    if(_hasTapEvent)
    {
        if(_isOn)
        {
            [self turnOff];
            
            if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(switchChanged:withSwitch:)])
            {
                [_eventDelegate switchChanged:NO withSwitch:self];
            }
        }
        else
        {
            [self turnOn];
            
            if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(switchChanged:withSwitch:)])
            {
                [_eventDelegate switchChanged:YES withSwitch:self];
            }
        }
    }
}


- (void)turnOff
{
    if(_isOn)
    {
        //开始动画
        [UIView animateWithDuration:_animateTime delay:0.0 options:0 animations:^
         {
             _line.backgroundColor=_offColor;
             _point.backgroundColor=_offColor;
             _button.layer.borderColor=_offColor.CGColor;
             _button.frame=CGRectMake(0, 0, _button.frame.size.width, _button.frame.size.height);
         }
         completion:^(BOOL finished)
         {
             if(finished)
             {
                 _isOn=NO;
             }
         }];
    }
}



- (void)turnOn
{
    if(!_isOn)
    {
        //开始动画
        [UIView animateWithDuration:_animateTime delay:0.0 options:0 animations:^
         {
             _line.backgroundColor=_onColor;
             _point.backgroundColor=_onColor;
             _button.layer.borderColor=_onColor.CGColor;
             _button.frame=CGRectMake(self.frame.size.width-_button.frame.size.width, 0, _button.frame.size.width, _button.frame.size.height);
         }
         completion:^(BOOL finished)
         {
             if(finished)
             {
                 _isOn=YES;
             }
         }];
    }
}



- (void)dealloc
{
    NSLog(@"KATSwitch is delloc!");
    
    [_buttonColor release];
    [_onColor release];
    [_offColor release];
    [_line release];
    [_button release];
    [_point release];
    
    [super dealloc];
}


@end
